//
//  UFOSXPLiveViewController.m
//  unrivalledForUniverse
//
//  Created by dllo on 1/11/16.
//  Copyright © 2016 dllo. All rights reserved.
//

#import "UFOSXPLiveViewController.h"
#import "UFOSXPHeroTimePlayPageViewController.h"
#import "LQCAFNetTool.h"
#import "UFOSXPPicPageViewController.h"

// collectionViewCell
#import "UFOSXPHeroTimePageCollectionViewCell.h"
#import "UFOSXPPicPageCollectionViewCell.h"
#import "UFOSXPMusicPageCollectionViewCell.h"

// model
#import "UFOSXPHeroTimeHotPageListModel.h"
#import "UFOSXPHeroTimeNewPageListModel.h"
#import "UFOSXPPicPageListModel.h"
#import "UFOSXPMusicPageListModel.h"

#define HEROTIMEHOTPAGEURL @"http://qt.qq.com/php_cgi/lol_mobile/hero_time/php/varcache_aj_get_hot_video.php?game_id=2103041&area=2&start=0&num=10&type=2"
#define ADDHEROTIMEHOTPAGEURL @"http://qt.qq.com/php_cgi/lol_mobile/hero_time/php/varcache_aj_get_hot_video.php?game_id=2103041&area=2&start=%ld&num=10&type=2"
#define HEROTIMENEWPAGEURL @"http://qt.qq.com/php_cgi/lol_mobile/hero_time/php/varcache_aj_get_hot_video.php?game_id=2103041&area=2&start=0&num=10&type=3"
#define PICPAGEURL @"http://api.lovebizhi.com/android_v3.php?a=tag&spdy=1&id=2207&name=%E8%8B%B1%E9%9B%84%E8%81%94%E7%9B%9F&device=samsung%28GT-I9500%29&uuid=d82cc1bb967ca456a4dcbf59e9b7109b&mode=2&client_id=1001&device_id=60572063&model_id=100&size_id=0&channel_id=1&screen_width=1080&screen_height=1920&bizhi_width=1080&bizhi_height=1920&version_code=76&language=zh-CN&mac=&original=0&p=1"
#define ADDPICPAGEURL @"http://api.lovebizhi.com/android_v3.php?a=tag&spdy=1&id=2207&name=%E8%8B%B1%E9%9B%84%E8%81%94%E7%9B%9F&device=samsung%28GT-I9500%29&uuid=d82cc1bb967ca456a4dcbf59e9b7109b&mode=2&client_id=1001&device_id=60572063&model_id=100&size_id=0&channel_id=1&screen_width=1080&screen_height=1920&bizhi_width=1080&bizhi_height=1920&version_code=76&language=zh-CN&mac=&original=0&p="
#define MUSICPAGEURL @"http://lol.zhangyoubao.com/apis/rest/AroundService/ring?page=1&i_=357512055380334&t_=1452655907658&p_=25319"
#define ADDMUSICPAGEURL @"http://lol.zhangyoubao.com/apis/rest/AroundService/ring?page=%ld&i_=357512055380334&t_=1452655907658&p_=25319"


@interface UFOSXPLiveViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UFOSXPHeroTimePageCollectionViewCellDelegate, UFOSXPPicPageCollectionViewCellDelegate, UFOSXPMusicPageCollectionViewCellDelegate>
@property (nonatomic, retain) UICollectionView *collectionV; // collectionView
@property (nonatomic, retain) UIView *viewB; // 滚动条
@property (nonatomic, retain) UIView *view0; // 滚动条的滚动条

// 数据数组
@property (nonatomic, retain) NSMutableArray *heroTimeHotArr;
@property (nonatomic, retain) NSMutableArray *heroTimeNewArr;
@property (nonatomic, retain) NSMutableArray *picListArr;
@property (nonatomic, retain) NSMutableArray *musicListArr;

// 刷新数据参数
@property (nonatomic, assign) NSInteger heroTimeHotPageNo;
@property (nonatomic, assign) NSInteger picListPageNo;
@property (nonatomic, assign) NSInteger musicListPageNo;

@end

@implementation UFOSXPLiveViewController

- (void)dealloc
{
    [_musicListArr release];
    [_picListArr release];
    [_heroTimeNewArr release];
    [_heroTimeHotArr release];
    [_view0 release];
    [_viewB release];
    [_collectionV release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    [self createView];
    [self getData];
}

// 创建视图
- (void)createView
{
    [self createCollectionV];
    [self createButtonView];

}

// 获取数据
- (void)getData
{
    [self getHeroTimeHotData];
    [self getPicPageData];
    [self getMusicPageData];
}

// 创建滚动条
- (void)createButtonView
{
    self.viewB = [[UIView alloc] initWithFrame:CGRectMake(0, 10, SCREENWIDTH, 34)];
    self.viewB.backgroundColor = [UIColor clearColor];
    self.viewB.userInteractionEnabled = YES;
    [self.navigationController.navigationBar addSubview:self.viewB];
    [self.view release];
    
    self.view0 = [UIView new];
    self.view0.backgroundColor = [UIColor colorWithRed:0.200 green:0.663 blue:1.000 alpha:1.000];
    [self.viewB addSubview:self.view0];
    [self.view0 mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(@32);
        make.left.equalTo(self.viewB);
        make.width.equalTo(@(SCREENWIDTH / 3));
        make.height.equalTo(@2);
        
    }];
    
    NSArray *titleArr = @[@"英雄时刻", @"精美壁纸", @"游戏铃声"];
    for (NSInteger i = 0; i < 3; i++) {
        UIButton *button = [UIButton new];
        [button setTitle:[titleArr objectAtIndex:i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        
        [button setTitleColor:[UIColor colorWithRed:0.200 green:0.663 blue:1.000 alpha:1.000] forState:UIControlStateSelected];
        button.titleLabel.font = [UIFont boldSystemFontOfSize:16.0];
        button.tag = 10001 + i;
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.viewB addSubview:button];
        
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.top.equalTo(@5);
            make.left.equalTo(@(SCREENWIDTH / 3 * i));
            make.width.equalTo(@(SCREENWIDTH / 3));
            make.height.equalTo(@20);
            
        }];
    }
    
    UIButton *button1 = (UIButton *)[self.viewB viewWithTag:10001];
    button1.selected = YES;
}

// 滚动条button触发方法
- (void)buttonAction:(UIButton *)sender
{
    NSInteger num = sender.tag - 10001;
    [self.collectionV scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:num inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}

// collection结束展示cell触发方法
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger index = self.collectionV.contentOffset.x / SCREENWIDTH;
    for (NSInteger i = 0; i < 3; i++) {
        UIButton *button = (UIButton *)[self.viewB viewWithTag:(10001 + i)];
        if (index == i) {
            button.selected = YES;
        } else {
            button.selected = NO;
        }
    }
    [UIView animateWithDuration:.3f animations:^{
        
        // 速度曲线
        [UIView setAnimationCurve:UIViewAnimationCurveLinear];
        
        // 重复次数
        [UIView setAnimationRepeatCount:1.f];
        
        self.view0.frame = CGRectMake(SCREENWIDTH / 3 * index, 32, SCREENWIDTH / 3, 2);
    }];
}

// 创建collectionView
- (void)createCollectionV
{
    UICollectionViewFlowLayout *flowL = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    flowL.minimumLineSpacing = 0;
    flowL.minimumInteritemSpacing = 0;
    flowL.headerReferenceSize = CGSizeMake(0, 0);
    flowL.footerReferenceSize = CGSizeMake(0, 0);
    
    flowL.itemSize = CGSizeMake(SCREENWIDTH, SCREENHEIGHT - NCHEIGHT - TABBARHEIGHT);
    flowL.sectionInset = UIEdgeInsetsMake(NCHEIGHT, 0, TABBARHEIGHT, 0);
    flowL.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionV = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowL];
    self.collectionV.backgroundColor = [UIColor clearColor];
    self.collectionV.pagingEnabled = YES;
    self.collectionV.delegate = self;
    self.collectionV.dataSource = self;
    self.collectionV.bounces = NO;
    
    [self.view addSubview:self.collectionV];
    [self.collectionV release];
    
    [self.collectionV mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.mas_equalTo(self.view);
        
    }];
    
    [self.collectionV registerClass:[UFOSXPHeroTimePageCollectionViewCell class] forCellWithReuseIdentifier:@"heroTimePageCell"];
    [self.collectionV registerClass:[UFOSXPPicPageCollectionViewCell class] forCellWithReuseIdentifier:@"picPageCell"];
    [self.collectionV registerClass:[UFOSXPMusicPageCollectionViewCell class] forCellWithReuseIdentifier:@"musicPageCell"];
}

// 单元数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 3;
}
// 自定义cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (0 == indexPath.item) {
        UFOSXPHeroTimePageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"heroTimePageCell" forIndexPath:indexPath];
        cell.delegate = self;
        if (self.heroTimeHotArr) {
            cell.hotDataArr = self.heroTimeHotArr;
        }
        if (self.heroTimeNewArr) {
            cell.NewDataArr = self.heroTimeNewArr;
        }
        return cell;
    }
    if (1 == indexPath.item) {
        UFOSXPPicPageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"picPageCell" forIndexPath:indexPath];
        cell.delegate = self;
        if (self.picListArr) {
            cell.picListArr = self.picListArr;
        }
        return cell;
    }
    if (2 == indexPath.item) {
        UFOSXPMusicPageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"musicPageCell" forIndexPath:indexPath];
        cell.delegate = self;
        if (self.musicListArr) {
            cell.musicListArr = self.musicListArr;
        }
        return cell;
    }

    return nil;
}

// 最热数据
- (void)getHeroTimeHotData
{
    [LQCAFNetTool getNetWithURL:HEROTIMEHOTPAGEURL body:nil headFile:nil responseStyle:LQCJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *arr = [dic objectForKey:@"list"];
        self.heroTimeHotArr = [UFOSXPHeroTimeHotPageListModel baseModelWithArr:arr];
        self.heroTimeHotPageNo = 0;
        [self.collectionV reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}

// 最热数据上拉加载
- (void)addHeroTimeHotDataWithPageNo:(NSInteger)pageNo
{
    [LQCAFNetTool getNetWithURL:[NSString stringWithFormat:ADDHEROTIMEHOTPAGEURL, pageNo * 10] body:nil headFile:nil responseStyle:LQCJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *arr = [dic objectForKey:@"list"];
        NSMutableArray *tempArr = [UFOSXPHeroTimeHotPageListModel baseModelWithArr:arr];
        [self.heroTimeHotArr addObjectsFromArray:tempArr];
        if (tempArr.count != 0) {
            self.heroTimeHotPageNo += 1;
        }
        [self.collectionV reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}

// 最新数据
- (void)getHeroTimeNewData
{
    [LQCAFNetTool getNetWithURL:HEROTIMENEWPAGEURL body:nil headFile:nil responseStyle:LQCJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *arr = [dic objectForKey:@"list"];
        self.heroTimeNewArr = [UFOSXPHeroTimeNewPageListModel baseModelWithArr:arr];
        
        [self.collectionV reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}

// cell1协议
- (void)getHotPageDataAgain
{
    [self getHeroTimeHotData];
}
- (void)getNewPageDataAgain
{
    [self getHeroTimeNewData];
}
- (void)getMoreHotPageData
{
    [self addHeroTimeHotDataWithPageNo:self.heroTimeHotPageNo + 1];
}
- (void)enterPlayPageFromHotWithIndex:(NSInteger)index
{
    UFOSXPHeroTimeHotPageListModel *model = [self.heroTimeHotArr objectAtIndex:index];
    UFOSXPHeroTimePlayPageViewController *ViewC = [[UFOSXPHeroTimePlayPageViewController alloc] init];
    ViewC.titleName = model.game_name;
    ViewC.vid = model.vid;
    UINavigationController *viewNC = [[UINavigationController alloc] initWithRootViewController:ViewC];
    viewNC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:viewNC animated:YES completion:^{
        
    }];
    [viewNC release];
    [ViewC release];
}
- (void)enterPlayPageFromNewWithIndex:(NSInteger)index
{
    UFOSXPHeroTimeNewPageListModel *model = [self.heroTimeNewArr objectAtIndex:index];
    UFOSXPHeroTimePlayPageViewController *ViewC = [[UFOSXPHeroTimePlayPageViewController alloc] init];
    ViewC.titleName = model.game_name;
    ViewC.vid = model.vid;
    UINavigationController *viewNC = [[UINavigationController alloc] initWithRootViewController:ViewC];
    viewNC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:viewNC animated:YES completion:^{
        
    }];
    [viewNC release];
    [ViewC release];
}


// 壁纸数据
- (void)getPicPageData
{
    [LQCAFNetTool getNetWithURL:PICPAGEURL body:nil headFile:nil responseStyle:LQCJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *arr = [dic objectForKey:@"data"];
        self.picListArr = [NSMutableArray array];
        for (NSMutableDictionary *dic1 in arr) {
            NSMutableDictionary *dic2 = [dic1 objectForKey:@"image"];
            UFOSXPPicPageListModel *model = [[UFOSXPPicPageListModel alloc] init];
            model.diy = [dic2 objectForKey:@"diy"];
            [self.picListArr addObject:model];
            [model release];
        }
        self.picListPageNo = 1;
        [self.collectionV reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}

// 壁纸刷新
- (void)addPicPageDataWithPageNo:(NSInteger)pageNo
{
    [LQCAFNetTool getNetWithURL:[ADDPICPAGEURL stringByAppendingString:[NSString stringWithFormat:@"%ld", pageNo]] body:nil headFile:nil responseStyle:LQCJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *arr = [dic objectForKey:@"data"];
        NSMutableArray *tempArr = [NSMutableArray array];
        for (NSMutableDictionary *dic1 in arr) {
            NSMutableDictionary *dic2 = [dic1 objectForKey:@"image"];
            UFOSXPPicPageListModel *model = [[UFOSXPPicPageListModel alloc] init];
            model.diy = [dic2 objectForKey:@"diy"];
            [tempArr addObject:model];
            [model release];
        }
        [self.picListArr addObjectsFromArray:tempArr];
        if (tempArr.count != 0) {
            self.picListPageNo += 1;
        }
        [self.collectionV reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}

// cell2协议
- (void)getMorePicPageListData
{
    [self addPicPageDataWithPageNo:self.picListPageNo + 1];
}
- (void)enterPicDetailPageWithIndex:(NSInteger)index
{
    UFOSXPPicPageViewController *viewC = [[UFOSXPPicPageViewController alloc] init];
    viewC.picDataArr = self.picListArr;
    viewC.index = index;
    viewC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:viewC animated:YES completion:^{
        
    }];
    [viewC release];
}


// 铃声数据
- (void)getMusicPageData
{
    [LQCAFNetTool getNetWithURL:MUSICPAGEURL body:nil headFile:nil responseStyle:LQCJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *arr = [dic objectForKey:@"data"];
        self.musicListArr = [UFOSXPMusicPageListModel baseModelWithArr:arr];
        self.musicListPageNo = 1;
        [self.collectionV reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}
// 铃声刷新
- (void)addMusicPageDataWithPageNo:(NSInteger)pageNo
{
    [LQCAFNetTool getNetWithURL:[NSString stringWithFormat:ADDMUSICPAGEURL, pageNo] body:nil headFile:nil responseStyle:LQCJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *arr = [dic objectForKey:@"data"];
        NSMutableArray *tempArr = [UFOSXPMusicPageListModel baseModelWithArr:arr];
        
        [self.musicListArr addObjectsFromArray:tempArr];
        if (tempArr.count != 0) {
            self.musicListPageNo += 1;
        }
        [self.collectionV reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}

// cell3协议
- (void)getMoreMusicPageData
{
    [self addMusicPageDataWithPageNo:self.musicListPageNo + 1];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
